# bs模拟器  分辨率 1280*720  副本 镜湖的地下遗迹
import random

from PyGameAutoAndroid import *


def wait(t):
    delta = t / 10
    time.sleep(random.uniform(t, t + delta))


def find_color_click(color, x, y, cx=None, cy=None):
    if cx is None:
        cx = x
    if cy is None:
        cy = y
    while True:
        ret = xp.matchColor(color, x, y)
        if not ret:
            wait(0.1)
            continue
        wait(0.3)
        xp.tap(cx, cy, 0.1)
        break


def find_no_color_click(color, x, y, cx=None, cy=None):
    if cx is None:
        cx = x
    if cy is None:
        cy = y
    while True:
        ret = xp.matchColor(color, x, y)
        if ret:
            wait(0.1)
            continue
        wait(0.3)
        xp.tap(cx, cy)
        break


def open_map():
    xp.console("尝试开启地图")
    find_color_click("#DADCD9", 480, 599)
    # find_color_click("#E2D57E", 1077, 140, cx=471, cy=611)
    # while True:
    #     ret = xp.matchColor("#DADCD9", 480, 599)
    #     if not ret:
    #         wait(0.1)
    #         continue
    #     wait(1)
    #     xp.tap(714, 464)
    #     break


def goto_town():
    xp.console("尝试进城")
    find_color_click("#C7C2AE", 556, 249)
    find_color_click("#2D637B", 1048, 656)
    find_color_click("#2A5D74", 848, 484)


def heal_self():
    xp.console("尝试回血")
    find_color_click("#FFFFFF", 650, 244)
    find_color_click("#ECE5DD", 752, 280)
    wait(1)
    # find_color_click("#39687E", 796, 505)
    # wait(0.5)
    # xp.tap(480, 599)
    while True:
        ret = xp.matchColor("#E2D57E", 1081, 137)
        if ret:
            wait(1)
            xp.tap(480, 599)
            break
        else:
            wait(0.1)
            xp.tap(796, 506)
            continue
    xp.console("尝试去野外")
    find_color_click("#C7C2AE", 722, 464)
    find_color_click("#265A72", 988, 645)
    find_color_click("#2A5D74", 848, 484)


def moving():
    xp.console("左右移动中")
    xp.swipe(1200, 700, 1000, 700)
    wait(0.5)
    xp.swipe(1000, 700, 1200, 700)
    wait(0.5)


def is_battle():
    ret = xp.matchColor("#FFFFFF", 1121, 643)
    xp.console("判断战斗是否开始")
    if ret:
        xp.console("战斗开始")
        return True
    else:
        return False


def battle_sequence():
    xp.console("选择技能")
    find_color_click("#548654", 1204, 81)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#548654", 1204, 225)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#548654", 1204, 367)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#548654", 1204, 513)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#FFFFFF", 1083, 643, cx=864, cy=643)
    find_color_click("#FFFFFF", 1083, 643)
    i = 1
    while True:
        xp.console("判断战斗是否结束")
        ret = xp.matchColor("#F0EBE8", 143, 138)
        if ret:
            xp.console("战斗结束")
            wait(0.1)
            xp.tap(756, 357)
            break
        else:
            xp.console("战斗未结束")
            wait(0.5)
            xp.tap(864, 643)
            xp.tap(1083, 643)


while True:
    count = 0
    req_init = True
    while True:
        if count == 0 and req_init:
            xp.console("打开小地图001")
            find_color_click("#E2D57E", 1075, 137)
            xp.console("选择目标前进")
            find_color_click("#E1E1E1", 282, 295)
            wait(3)
            req_init = False
        if count == 5:
            break
        moving()
        if is_battle() is True:
            battle_sequence()
            count += 1
        else:
            continue
    open_map()
    goto_town()
    heal_self()
